import requestUtil from "@/utils/requestUtil";

const SEALMEAL_BASE_URL = "/business/saleMeal";
import { IObject } from "@/api/common/common";

const SealMealAPI = {
  /** 获取分页数据 */
  getPage(queryParams?: SealMealPageQuery) {
    return requestUtil.get<PageResult<SealMealPageVO[]>>(
      `${SEALMEAL_BASE_URL}/loadPage`,
      queryParams
    );
  },

  /** 获取所有套餐可以配置的数据 */
  loadConfigAll(warehouseId?: string) {
    return requestUtil.get<SealMealPageVO[]>(
      `${SEALMEAL_BASE_URL}/loadConfigAll?warehouseId=${warehouseId}`
    );
  },
  /**
   * 获取表单数据
   *
   * @param id ID
   * @returns 表单数据
   */
  getFormData(id: string) {
    return requestUtil.get<SealMealForm>(`${SEALMEAL_BASE_URL}/load/${id}`);
  },

  /** 添加 */
  create(data: SealMealForm) {
    return requestUtil.post<SealMealForm>(`${SEALMEAL_BASE_URL}/add`, data);
  },

  /**
   * 更新
   *
   * @param id ID
   * @param data 表单数据
   */
  update(data: SealMealForm) {
    return requestUtil.post<SealMealForm>(`${SEALMEAL_BASE_URL}/update`, data);
  },

  /**
   * 批量删除，多个以英文逗号(,)分割
   *
   * @param ids ID字符串，多个以英文逗号(,)分割
   */
  deleteByIds(ids: string) {
    return requestUtil.get<number>(`${SEALMEAL_BASE_URL}/batchDelete?ids=${ids}`);
  },

  transToGoods(categoryId: string, ids: string[]) {
    let data = { categoryId: categoryId, salesIds: ids };
    return requestUtil.post<number>(`${SEALMEAL_BASE_URL}/transToGoods`, data);
  },
};

export default SealMealAPI;

/** 分页查询参数 */
export interface SealMealPageQuery extends PageQuery {
  /** 搜索关键字 */
  keywords?: string;
}

/** 分页对象 */
export interface SealMealPageVO {
  /** ID */
  id?: string;
  /** 名称 */
  name?: string;
  /** 编码 */
  code?: string;
  /**  */
  price?: string;
  unit?: string;
  type?: SaleMealItemType;
  remark?: string;
  /**总成本价 */
  totalCostPrice?: string;
  /** 最大配置数量 */
  maxConfigNum?: number;
  /** 主材数量 */
  mainMaterialNum: number;
  /** 辅材数量 */
  subMaterialNum: number;
  /** 定额数量 */
  normMaterialNum: number;
}

export interface SealMealInfo {
  /** ID */
  id?: string;
  /** 名称 */
  name?: string;
  /**  */
  price?: number;

  remark?: string;
}

export interface SealMealItem {
  /** ID */
  id?: string;
  /** 套餐 */
  saleMealId?: string;
  /**  */
  resourceId?: string;
  name?: string;
  type?: SaleMealItemType;
  price?: string;
  costPrice?: string;
  num?: number;
  spec?: string;
  unit?: string;
  code?: string;
}

/** 表单对象 */
export interface SealMealForm {
  items: SealMealItem[];
  id?: string;
  /** 名称 */
  name?: string;
  /** 编码 */
  code?: string;
  /**  */
  price?: number | string;
  unit?: string;
  type?: SaleMealItemType;
  remark?: string;
}

export enum SaleMealItemType {
  MAIN = "MAIN",
  SUB = "SUB",
  INSTRUMENT = "INSTRUMENT",
  NORM = "NORM",
  OTHER = "OTHER",
}

export const SaleMealTypeLabel: IObject = {
  [SaleMealItemType.MAIN]: "主材",
  [SaleMealItemType.SUB]: "辅材",
  [SaleMealItemType.INSTRUMENT]: "器具",
  [SaleMealItemType.NORM]: "定额",
  [SaleMealItemType.OTHER]: "其他",
};
